import 'package:Smart/utils/routers/page_jump_service.dart';
import 'package:flutter/material.dart';
//屏幕适配
import 'package:Smart/utils/screen_unit/gs_screen_util.dart';
import '../res/colors_res.dart';
import 'package:flutter/cupertino.dart';
import 'gs_bluetooth_dialog.dart';
import '../utils/gs_permission_util.dart';
import 'package:permission_handler/permission_handler.dart';
//拨打电话
import 'package:url_launcher/url_launcher.dart';

//一键呼出弹出框
class GSCallOut extends StatefulWidget {
  GSCallOut({Key key}) : super(key: key);

  @override
  _GSCallOutState createState() => _GSCallOutState();
}

class _GSCallOutState extends State<GSCallOut> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        child: Column(
          children: <Widget>[
            Container(
              alignment: Alignment.center,
              child: Container(
                margin: EdgeInsets.only(top: 700.w),
                width: 278.w,
                height: 48.w,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(26.w),
                    color: Color(0xffD7E600),
                    border: Border.all(
                      width: 1,
                      color: Color(0xffD1E000),
                    )),
                child: Center(
                  child: GestureDetector(
                    //
                    //onTap: checkPermission(context),
                    onTap: () {
                      //检查是否有拨打电话权限
                      checkPermission(context);
                    },
                    child: Text(
                      '客服电话 400-123-456',
                      style: TextStyle(
                          fontSize: 18.w,
                          color: Colors.black,
                          decoration: TextDecoration.none),
                    ),
                  ),
                ),
              ),
            ),
            Container(
              alignment: Alignment.center,
              //弹框'取消'按钮
              child: Container(
                margin: EdgeInsets.only(top: 30.w),
                width: 278.w,
                height: 48.w,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(26.w),
                    color: Color(0xffffffff),
                    border: Border.all(
                      width: 1,
                      color: Color(0xffD1E000),
                    )),
                child: Center(
                  child: GestureDetector(
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: Text(
                      '取消',
                      style: TextStyle(
                          fontSize: 18.w,
                          color: ColorsRes.color_141413,
                          decoration: TextDecoration.none),
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

//判断是否有拨打电话权限,没有则申请权限
checkPermission(context) async {
  //获取状态
  var status = await Permission.phone.status;
  // print(status);
  //判断
  if (!status.isGranted) {
    //获取权限
    bool permission =
        await GSPermissionUtil.requestPhonePermission(context: context);
    if (permission) {
      launchPhone();
    }
  } else {
    launchPhone();
  }
}

//唤起手机打电话功能
launchPhone() async {
  const url = 'tel:' + '400 123 456';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw '电话$url访问异常';
  }
}
